/**
 * @fileOverview 多边形
 * @author goodzsq@gmail.com
 */

var BaseShape = gg.module('shape.Base');

var Polygon = BaseShape.extend({
    area: function() {
        var points = this.get('points');
        var area = 0, i;
        for (i = 0; i < points.length - 1; i++) {
            area += (points[i].x * points[i + 1].y - points[i + 1].x * points[i].y);
        }
        area /= 2.0;
        return Math.abs(area);
    },
    hasPoint: function(p) {
        var points = this.get('points');
        var i, j, len, c = false;
        len = points.length;
        for (i = 0, j = len - 2; i < len - 1; j = i++) {
            if (((points[i].y > p.y) !== (points[j].y > p.y))
                    && (p.x < (points[j].x - points[i].x) * (p.y - points[i].y) / (points[j].y - points[i].y) + points[i].x)) {
                c = !c;
            }
        }
        return c;
    }
});

Polygon.metadata = _.extend({}, BaseShape.metadata, {
    points: {
        label: '点集',
        type: 'datatype.Array',
        defaultValue: []
    }
});

module.exports = Polygon;